#include <1602.h>
#include <intrins.h>
#include <string.h>
sbit rs=P2^5;
sbit rw=P2^6;
sbit e=P2^7;
bit lcd_busy();
void delay_4us()
{
	_nop_();
	_nop_();
	_nop_();
	_nop_();
}

void delay(uchar ms)
{
	uchar i,j;
	for (i=ms;i>0;i--)
		for (j=120;j>0;j--);
}

bit Lcd_busy()
{
	uchar k;
	P0=0xff;
	rs=0;
	rw=1;
	e=1;
	delay_4us();
	k=P0;
	e=0;
	return(k&0x80)?1:0;
}

void Lcd_WriteCommand(uchar cmd)
{
	while (lcd_busy());
	rs=0;
	rw=0;
	e=0;
	_nop_();
	_nop_();
	P0=cmd;
	delay_4us();
	e=1;
	delay_4us();
	e=0;
}

void Lcd_WriteData(uchar dat)
{
	while (lcd_busy());
	rs=1;
	rw=0;
	e=0;
	P0=dat;
	delay_4us();
	e=1;
	delay_4us();
	e=0;
}

void Lcd_Show_String(uchar r, uchar c,uchar *str)
{
	uchar i = 0;
	code uchar DDRAM[] = {0x80,0xC0};		
	Lcd_WriteCommand(DDRAM[r] | c);		

	for ( i = 0; str[i] && i < 16 ;i++)		
	  Lcd_WriteData(str[i]);

	for (; i < 16; i++)	 					
	  Lcd_WriteData(' ');
}

void Lcd_Init()
{
	Lcd_WriteCommand(0x38);
	delay(1);
	Lcd_WriteCommand(0x01);
	delay(1);
	Lcd_WriteCommand(0x06);
	delay(1);
	Lcd_WriteCommand(0x0c);
	delay(1);
}

